package d0327;

import java.util.*;

public class TestStudent {



        private ArrayList<Student> arrayList = new ArrayList<Student>();

        public void save() {
            Random random = new Random();
            //保存40位同学的姓名、学号、成绩
            for (int i = 0, j = 10; i <= 60; i++) {
                int id = 180201 + i;
                String name = "同学" + j++;
                int score = random.nextInt(51) + 50;
                this.arrayList.add(new Student(id, name, score));
            }
        }

        public void sort() {
            Comparator<Student> comparator = new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o2.getScore() > o1.getScore() ? 1 : o2.getScore() < o1.getScore() ?
                            -1 : o1.getId() - o2.getId();
                }
            };
            Collections.sort(arrayList, comparator);
        }

        public void print() {
            Iterator<Student> iterator = this.arrayList.iterator();
            while (iterator.hasNext()) {
                Student value = iterator.next();
                System.out.println("学号：" + value.getId() + "姓名：" + value.getName() +
                        "成绩：" + value.getScore());
            }
        }

        public static void main(String[] args) {
            TestStudent testStudent = new TestStudent();

            testStudent.save();
            testStudent.sort();
            testStudent.print();
        }
    }

